package cn.tedu.demo.service.impl;

import cn.tedu.demo.entity.Book;
import cn.tedu.demo.entity.Travel;
import cn.tedu.demo.mapper.TravelMapper;
import cn.tedu.demo.service.TravelService;
import cn.tedu.demo.vo.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class TravelServiceImpl implements TravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Override
    public PageInfo getByPage(PageInfo pageInfo) {
        IPage<Travel> page=new Page<>(pageInfo.getPageNum(),pageInfo.getPageSize());
        boolean flag= StringUtils.hasLength(pageInfo.getQuery());
        QueryWrapper<Travel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(flag, "title",pageInfo.getQuery());
        page=travelMapper.selectPage(page, queryWrapper);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRows(page.getRecords());
        return pageInfo;
    }

    @Override
    @Transactional
    public void delete(Long id) {
        travelMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Integer> ids) {
      travelMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional
    public void update(Travel travel) {
        travelMapper.updateById(travel);
    }

    @Override
    @Transactional
    public void save(Travel travel) {
       travelMapper.insert(travel);
    }

    @Override
    public Book checkBook(Integer id) {
        return null;
    }

}
